<?php


namespace app\common\model;


use think\Db;

class Aftersales extends Base
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'ddt_after_sales';
    // 主键ID
    protected $pk = 'a_s_id';

    //订单售后状态
    public static $afterstatus = [
        '1' => '未处理',
        '2' => '已处理',
    ];

    //选择售后类型
    public static $aftertype = [
        '1' => '售后咨询',
        '2' => '申请退款',
    ];

    // 当前退款状态
    public static $refundstatus = [
        '1' => '未退款',
        '2' => '已退款',
    ];

    /**
     * @param array $where  // 查询的条件
     * @param string $item  // 查询的参数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function getOne($where=[1], $item='*')
    {
        $rs = $this
            ->field($item)
            ->where($where)
            ->find();

        return $rs;
    }

    //新增、编辑(后台操作)
    function edit($params, $id=0){
        if($id>0){
            // 更新条件
            $where = ['a_s_id' => $id];
            $params['update_time'] = time();
            $rs = $this->allowField(true)->save($params, $where);
        }else{
            // 新增数据
            $params['add_time'] = time();
            $this->allowField(true)->save($params);
            $rs = $this->a_s_id;
        }
        if ($rs === false) {
            $this->setError(-1, '操作失败');
            return false;
        }
        return $rs;
    }

//    获取商品售后管理申请列表
    function get_aftersales_list($cond,$field = 'as.*',$orderby = 'as.a_s_id desc',$limit = 10,$join = [],$is_transform = false,$is_getdetails = false)
    {
        $where = null;
        if(!empty($cond['user_id'])){
            $where[] = ['as.user_id','=',$cond['user_id']];
        }
        if(!empty($cond['shop_id'])){
            $where[] = ['as.shop_id','=',$cond['shop_id']];
        }
        if(!empty($cond['status'])){
            $where[] = ['as.a_s_status','=',$cond['status']];
        }
        if(!empty($cond['a_s_type'])){
            $where[] = ['as.a_s_type','=',$cond['a_s_type']];
        }
        if(!empty($cond['is_show'])){
            $where[] = ['as.is_show','=',$cond['is_show']];
        }
        if(!empty($cond['is_del'])){
            $where[] = ['o.is_del','=',$cond['is_del']];
        }
        if(!empty($cond['shop_name'])){
            $where[] = ['s.shop_name','like',"%".$cond['shop_name']."%"];
        }
        if(!empty($cond['product_name'])){
            $OrderDetailModel = new OrderDetail();
            $orderids = $OrderDetailModel
                ->where('user_id','=',$cond['user_id'])
                ->where('product_name','like','%'.$cond['product_name'].'%')
                ->distinct(true)
                ->column('order_id');
            $where[] = ['as.order_id','in',implode(',',$orderids)];
        }

        $data = $this
            ->alias('as')
            ->join($join)
            ->field($field)
            ->where($where)
            ->order($orderby);
        if($is_getdetails){
            $with = [
                'details'
            ];
            $data = $data->with($with);
        }
        $data = $data->paginate($limit)->each(function($value) use($is_transform){
                if($is_transform){
                    $value['order_product_details'] = json_decode($value['order_product_details'],true);
                }
                $value['images'] = explode(',',$value['images']);
                return $value;
            });
        return $data;
    }

    /**
     * 建立和OrderDetail的关联
     * hasMany方法的参数包括:hasMany('关联模型名','外键名','主键名',['模型别名定义']);
     */
    public function details(){
        return $this->hasMany('OrderDetail','order_id','order_id');
    }

//    操作撤销申请
    function operatewithdrawal($condition){
        $where[] = ['a_s_id','=',$condition['a_s_id']];

        if(!empty($condition['user_id'])){
            $where[] = ['user_id', '=', $condition['user_id']];
        }
        $after = $this->getOne($where);
        if(empty($after)){
            $this->setError(-1,'用户不存在该申请');
            return false;
        }
        $param = [
            'is_show' => 0,
            'update_time' => time(),
        ];
        $rs = $this->where("a_s_id",'=',$after['a_s_id'])->update($param);
        if($rs === false){
            $this->setError(-1,'操作失败');
            return false;
        }
        return true;
    }
}